﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using MSCRMSendReportInEmail.wsSSRS;
using System.IO;

namespace MSCRMSendReportInEmail
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(String.Format("Start MSCRMSendReportInEmail: {0}", DateTime.Now));
            //get Crm service
            Console.WriteLine("Get Crm service");
            MSCrmMethods crm = new MSCrmMethods();
            //Get parameters
            Console.WriteLine("Get parameters from xml");
            ProgramUtil data = new ProgramUtil();
            data.GetParameters(args);
            
            //Get report from reporting services
            Console.WriteLine("Get report from reporting services");
            string encodedReport = ReportMethods.TurnToPdf(data.ReportFullPath, data.ReportFormat);

            //create email
            Console.WriteLine("create email");
            var email = crm.CreateEmailFromTemplate(data);
            //attach report
            Console.WriteLine("create attachment");
            crm.CreateAttachment(email.Id, encodedReport, data);
            //send email
            Console.WriteLine("Send email");
            crm.SendEmail(email.Id);
        }
    }
}
